import {messages} from "share/common";
import React, { Component } from 'react'
import DataHoc from "./../template/data-hoc"
import { Spin, message } from 'antd'
import { AdvancedTable } from "components/index";
import httpFetch from "share/httpFetch";

class RefundCheck extends Component {

  key = 'refund-check';

  state = {
    searchForm: this.props.getSearchForm(this.key),
    loading: false,
    sortDTOs: []
  };

  service = (page, size, searchParams) => {
    const { sortDTOs } = this.state;
    return httpFetch.post(`/payment/api/cash/transaction/details/refundCheckList`, {
      page, size, ...searchParams,
      sortProperty: (sortDTOs[0] || {}).property,
      sortDirection: (sortDTOs[0] || {}).direction
    })
  };

  getColumns = () => {
    return this.props.getColumns(this.key).concat([{
      title: messages('common.operation')/*操作*/, dataIndex: 'operator',
      render: (value, record) => (
        <span>
          <a onClick={() => this.handleOperator(record, '01')}>{messages('common.pass')/*通过*/}</a>
          <span className='ant-divider'/>
          <a onClick={() => this.handleOperator(record, '00')}>{messages('common.reject')/*驳回*/}</a>
        </span>
      )
    }])
  };

  handleOperator = (record, checkStatus) => {
    this.setState({ loading: true });
    httpFetch.post(`/payment/api/cash/transaction/details/refundCheck`, {
      detailIds: [record.id], checkStatus
    }).then(() => {
      message.success(messages('common.operate.success')/*操作成功*/);
      this.setState({ loading: false }, this.advancedTableRef.getList)
    }).catch(e => {
      message.error(e.response.data.message);
      this.setState({ loading: false });
    })
  };

  render() {
    const { loading, searchForm } = this.state;
    const { handleTableChange } = this.props;
    return (
      <div style={{ height: 'calc(100vh - 110px)' }}>
        <Spin spinning={loading}>
          <AdvancedTable
            title={messages('finance-4.key87')/*退票复核*/}
            tableName='refund-check'
            service={this.service}
            onRef={ref => this.advancedTableRef = ref}
            searchForm={searchForm}
            rowKey="id"
            columns={this.getColumns()}
            onChange={handleTableChange}
          />
        </Spin>
      </div>
    )
  }

}

export default DataHoc(RefundCheck)
